import java.util.Stack;

public class Solution4 {
    public static void main(String[] args) {
    System.out.println(decodeString("3[a2[c]]"));
    }
    public static String decodeString(String ss) {
        Stack<String> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        stack1.push("");
        int i = 0;
        int n = ss.length();
        char[] s = ss.toCharArray();
        while(i < n) {
            if(s[i] >= '0' && s[i] <= '9') {
                int tmp = 0;
                while(i < n && s[i] >= '0' && s[i] <= '9') {
                    tmp = tmp * 10 + (s[i] - '0');
                    i++;
                }
                stack2.push(tmp);
            }else if(s[i] == '[') {
                String tmp = "";
                i++;
                while(i < n && s[i] >= 'a' && s[i] <= 'z') {
                    tmp += s[i];
                    i++;
                }
                stack1.push(tmp);
            }else if(s[i] == ']') {
                int num = stack2.pop();
                String str = stack1.pop();
                String ret = "";
                for(int j = 0; j < num; j++) {
                    ret += str;
                }
                stack1.push(stack1.pop() + ret);
                i++;
            }else {
                stack1.push(stack1.pop() + s[i]);
                i++;
            }
        }
        return stack1.pop();
    }
}
